//***********************************************************************************
// Function for player-NPC interactions and NPC reply textbox queue

// Author: Aravindh U095232L

//***********************************************************************************
function message()
{

	this.timer =3 ;
	this.play = new Image();
	this.play.src = "Images/npc.png";

	this.spy_image = new Image();
	this.spy_image.src = "Images/spy.png";
	this.rebel_image = new Image();
	this.rebel_image.src = "Images/rebel.png";
	this.merchant_image = new Image();
	this.merchant_image.src = "Images/merchant.png";
	this.army_image = new Image();
	this.army_image.src = "Images/army.png";
	this.citizen_image = new Image();
	this.citizen_image.src = "Images/citizen.png";

	this.list = new Image();
	this.list.src = "assets/Text/list.png";
	this.box = new Image();
	this.box.src = "assets/Text/box.png";
	this.bubble = new Image();
	this.bubble.src = "assets/Text/bubble.png";
	this.done = 1;
	this.control = 0;
	

	this.text_queue = function(id,msg)
	{
		id_queue.push(id);
		msg_queue.push(msg);
		//msg_queue.push(msg);
		//console.log(msg_queue);
	};

	this.display_img = function(id)
	{
		if(id=="Spy")
		{
			txt.drawImage(this.spy_image, 6, 3,35,50);
		}
		else if(id=="Merchant")
		{
			txt.drawImage(this.merchant_image, 6, 3,35,50);
		}
		else if(id=="Army")
		{
			txt.drawImage(this.army_image, 6, 3,35,50);
		}
		else if(id=="Rebel")
		{
			txt.drawImage(this.rebel_image, 6, 3,35,50);
		}
		else if(id=="Citizen")
		{
			txt.drawImage(this.citizen_image, 6, 3,35,50);
		}
		else if(id == "blackMarket")
		{
			txt.drawImage(this.merchant_image, 6, 3,35,50);
		}
		else if(id == "tvStation2")
		{
			txt.drawImage(this.citizen_image, 6, 3,35,50);
		}

	};

	this.display_msg = function(id, msg)
	{	
		this.done = 0;
		final_msg = msg + "                                 ";
		temp_msg = final_msg.substr(0,(this.timer/3));
		txt.fillStyle    = '#D8D8D8';
		txt.font         = '20px Courier New';
		txt.fillText  (id+": "+temp_msg, 50, 30);

		this.timer++;

		if(temp_msg.length == final_msg.length)
			{
				this.done =1;
				this.timer = 0;
			}
	};

	this.displaytext = function()
	{	
		if(msg_queue.length == 0 && this.done ==1)
			{return;}

		if(this.done == 1)
		{	
		id_display = id_queue.shift();
		msg_display = msg_queue.shift(); 
		}
		//console.log("K "+msg_display);
		this.display_img(id_display);	
		this.display_msg(id_display,msg_display);	
	};

	this.display_list = function()
	{	
		var displayer=[];
		var mouse_click=-1;
		if(conv_type =="Spy")
			displayer = dialogues.rebelToSpyInteraction;
		else if(conv_type =="Army")
			displayer = dialogues.rebelToArmyInteraction;
		else if(conv_type =="Merchant")
			displayer = dialogues.rebelToMerchantInteraction;
		else if(conv_type =="Citizen")
			displayer = dialogues.rebelToCitizenInteraction;
		else if(conv_type =="blackMarket")
			displayer = dialogues.rebelToBlackMarketInteraction;
		else if(conv_type =="tvStation2")
			displayer = dialogues.rebelToTVStationInteraction;

		if(displayer.length)
		{
		dlg.drawImage(this.list, 0,0);
		dlg.fillStyle    = '#000000';
		dlg.font         = '17px Courier New';
		dlg.fillText  ("To "+conv_type+":", 150 , 40);

			for(var i =0;i<displayer.length;i++)
			{
			var x = 110 + this.box.width;
			var y = 80 + i*(this.list.height/6+20);

			dlg.save();
			dlg.globalAlpha = 0.4;
			if( mouse_x>110 && mouse_x<(this.box.width+110) )
				if( mouse_y>y && mouse_y<(this.list.height/6+y) )
				{	dlg.globalAlpha = 0.6;
					if(mouse_down && this.control==0)
					{	
						player.currentDialogue = displayer[i];
						this.control = 1;
						if(conv_type=="blackMarket")
						{
							Interactions.playerTalkingToBlackMarket(player);
						}
						else if(conv_type=="tvStation2")
						{
							Interactions.playerTalkingToTVStation(player); 
						}
						else
						Interactions.playerRebelInteractions(player,conv_object);
					}
				}
			dlg.drawImage(this.box, 15,y-20,this.list.width - 30, this.list.height/6);
			dlg.restore();

			dlg.fillStyle    = '#000000';
			dlg.font         = '20px Courier New';
			dlg.fillText  (displayer[i], 25, y+20);
			}
			//console.log(mouse_click);
		}	
	};

	this.speech_bubble = function(object)
	{
		ctx.drawImage(this.bubble, object.pos_x+7, object.pos_y-30, 50,30);
		ctx.fillStyle    = '#000000';
		ctx.font         = '15px Courier New';
		ctx.fillText  ("Hi!", object.pos_x+20,object.pos_y-12);
	};

	this.debug_text = function()
	{	
		dbg.fillStyle    = '#000000';
		dbg.font         = '15px Courier New';
		dbg.fillText  ("Global spread - Level 3", 40, 15);
        dbg.translate(0, 10);
		dbg.font         = '12px Courier New';
		dbg.fillText ("No of Army: "+army,10,30);
		dbg.fillText ("No of Rebel: "+rebel,10,45);

        var armyRatio = army / (army + rebel);
        dbg.fillStyle = "red";
        dbg.fillRect(10, 55, 200, 10);
        dbg.fillStyle = "green";
        dbg.fillRect(10, 55, 200 * armyRatio, 10);
        dbg.save();

        dbg.fillStyle = "black";
        dbg.translate(0, 50);
		dbg.fillText ("No of Spy: "+spy,10,50);
		dbg.fillText ("No of Merchant: "+merchant,10,60);

        dbg.translate(0, 20);
		dbg.fillText ("No of Citizen: "+citizen,10,70);
		dbg.fillText ("For war: "+for_war,10,80);
		dbg.fillText ("Against war: "+against_war,10,90);
        var for_ratio = for_war / citizen;
        var against_ratio = against_war / citizen;
        dbg.fillStyle = "grey";
        dbg.fillRect(10, 100, 200, 10);
        dbg.fillStyle = "green";
        dbg.fillRect(10, 100, 200 * for_ratio, 10);
        dbg.fillStyle = "red";
        dbg.fillRect(10+200-against_ratio*200, 100, against_ratio*200, 10);

        dbg.translate(0, 50);
        dbg.fillStyle = "black";
		dbg.fillText ("Army Fights: "+army_fight,10,100);
		dbg.fillText ("Rebel Fights: "+rebel_fight,10,110);
		dbg.fillText ("Army Weapons: "+army_weapons,10,120);
		dbg.fillText ("Rebel Weapons: "+rebel_weapons,10,130);

        dbg.translate(0, 20);
		dbg.fillText ("Army TV control: "+army_control.toFixed(2),10,140);
		dbg.fillText ("Rebel TV control: "+rebel_control.toFixed(2),10,150);

        dbg.fillStyle = "grey";
        dbg.fillRect(10, 160, 200, 10);
        dbg.fillStyle = "green";
        dbg.fillRect(10, 160, 200 * army_control, 10);
        dbg.fillStyle = "red";
        dbg.fillRect(10+200-rebel_control*200, 160, rebel_control*200, 10);

        dbg.translate(0, 40);
        dbg.fillStyle = "black";
		dbg.fillText ("Roadshow Sucess: "+show_ratio.toFixed(2),10,160);
		dbg.fillText ("Spy Influence Events: "+spy_influence,10,170);
		dbg.fillText ("Rebel Influence Events: "+rebel_influence,10,180);
        dbg.translate(0, -140);
        dbg.restore();
        dbg.translate(0, -10);


        var level2_info_canvas = document.getElementById("level2_canvas");
        var ctx = level2_info_canvas.getContext("2d");
        ctx.clearRect(0, 0, level2_info_canvas.width, level2_info_canvas.height);
        ctx.save();
        var adjacentGrids = map.getAdjcentGrids();

        ctx.translate(0, 10);
        for (var i=0; i < adjacentGrids.length; i++) {

            ctx.fillText("Grid X" + adjacentGrids[i].getIndexOfGrid().colIndex + " Y" + adjacentGrids[i].getIndexOfGrid().rowIndex, 10, 0);
            ctx.translate(0, 10);
            var npcs = adjacentGrids[i].getNPCs();
            ctx.save();
            for (var type in npcs) {
                for (var j=0; j<npcs[type].length; j++) {
                    var singleNPC = npcs[type][j];
                    singleNPC.draw(ctx, 0, 0, 64, 64);
                    ctx.fillText(singleNPC.getType(), 70, 12);
                    ctx.fillText(singleNPC.eventType, 70, 24);
                    if (type === "Army" || type === "Rebel") {
                        ctx.fillText("Weapon Level  " + singleNPC.weaponType, 70, 36);
                    } else {
                        ctx.fillText("No Weapon", 70, 36);
                    }
                    if (type === "Citizen") {
                        ctx.fillText("Influenced " + singleNPC.rateAtWhichCanBeInfluenced.toFixed(3), 70, 48);
                    } else if (type === "Rebel" || type === "Spy") {
                        ctx.fillText("Influence " + singleNPC.rateOfInfluence.toFixed(3), 70, 48);
                    } else if (type === "Army") {
                        ctx.fillText("War!", 70, 48);
                    } else if (type === "Merchant") {
                        ctx.fillText("Whatever!", 70, 48);
                    }
                    ctx.translate(200, 0);
                }
            }
            ctx.restore();
            ctx.translate(0, 100);
        }
        ctx.restore();
	};

	this.make_box = function(number)
	{

	}

}
